package bd1.obli2012.extapp;


import bd1.obli2012.framework.QueryCriteria;
import bd1.obli2012.framework.definicion.Tabla;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JTextField;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author favio.ortelli/guillermo.nasi
 */
public class DialogoFiltro extends javax.swing.JDialog {

    private Tabla tabla;
    private List<PanelFiltro> criterias;

    /**
     * Creates new form DialogoFiltro
     */
    public DialogoFiltro(AplicacionExterna parent, boolean modal, Tabla tabla) {
        super(parent, modal);
        initComponents();
        this.tabla = tabla;
        criteriasPanel.add(new PanelFiltro((tabla.getAttributes())));
        criteriasPanel.revalidate();
        criterias = new ArrayList<PanelFiltro>();
        pack();
        /*
         GridLayout layout = (GridLayout)getLayout();
         layout.setRows(layout.getRows()+1);
         */

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnAgregarCriteria = new javax.swing.JButton();
        btnQuitarCriteria = new javax.swing.JButton();
        criteriasPanel = new javax.swing.JPanel();
        btnAplicarFiltros = new javax.swing.JButton();
        btnLimpiar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setModal(true);
        setResizable(false);

        btnAgregarCriteria.setText("Agregar");
        btnAgregarCriteria.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarCriteriaActionPerformed(evt);
            }
        });

        btnQuitarCriteria.setText("Quitar");
        btnQuitarCriteria.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnQuitarCriteriaActionPerformed(evt);
            }
        });

        criteriasPanel.setLayout(new java.awt.GridLayout(1, 1, 1, 1));

        btnAplicarFiltros.setText("Aplicar");
        btnAplicarFiltros.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAplicarFiltrosActionPerformed(evt);
            }
        });

        btnLimpiar.setText("Limpiar");
        btnLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimpiarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(criteriasPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnAgregarCriteria, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnQuitarCriteria, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 88, Short.MAX_VALUE)
                        .addComponent(btnLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnAplicarFiltros, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAgregarCriteria)
                    .addComponent(btnQuitarCriteria)
                    .addComponent(btnAplicarFiltros)
                    .addComponent(btnLimpiar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(criteriasPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAgregarCriteriaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarCriteriaActionPerformed

        GridLayout layout = (GridLayout) this.criteriasPanel.getLayout();
        layout.setRows(layout.getRows() + 1);
        PanelFiltro filtro = new PanelFiltro(this.tabla.getAttributes());
        criterias.add(filtro);
        this.criteriasPanel.add(filtro);
        this.criteriasPanel.revalidate();
        this.pack();
    }//GEN-LAST:event_btnAgregarCriteriaActionPerformed

    private void btnQuitarCriteriaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnQuitarCriteriaActionPerformed

        if (this.criterias.size() > 0) {
            PanelFiltro p = criterias.get(criterias.size() - 1);
            this.criteriasPanel.remove(p);
            criterias.remove(p);
            GridLayout layout = (GridLayout) this.criteriasPanel.getLayout();
            layout.setRows(layout.getRows() - 1);
            this.criteriasPanel.revalidate();
            this.pack();
        }
    }//GEN-LAST:event_btnQuitarCriteriaActionPerformed

    private void btnAplicarFiltrosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAplicarFiltrosActionPerformed
        List<PanelFiltro> filtros = getAllFiltros(this);
        List<QueryCriteria> tempCriterias = new ArrayList<QueryCriteria>();
        for (PanelFiltro panelFiltro : filtros) {
            QueryCriteria qc = panelFiltro.getQueryCriteria();
            if(qc != null) {
                tempCriterias.add(qc);
            }
        }
        ((AplicacionExterna)getParent()).setCriteriasActivas(tempCriterias);
        ((AplicacionExterna)getParent()).actualizarTableModel(tabla);
    }//GEN-LAST:event_btnAplicarFiltrosActionPerformed

    private void btnLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimpiarActionPerformed
        
        while (this.criterias.size() > 0) {
            PanelFiltro p = criterias.get(criterias.size() - 1);
            this.criteriasPanel.remove(p);
            criterias.remove(p);
            GridLayout layout = (GridLayout) this.criteriasPanel.getLayout();
            layout.setRows(layout.getRows() - 1);
            this.criteriasPanel.revalidate();
            this.pack();
        }
        ((AplicacionExterna)getParent()).setCriteriasActivas(null);
        ((AplicacionExterna)getParent()).actualizarTableModel(tabla);
    }//GEN-LAST:event_btnLimpiarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregarCriteria;
    private javax.swing.JButton btnAplicarFiltros;
    private javax.swing.JButton btnLimpiar;
    private javax.swing.JButton btnQuitarCriteria;
    private javax.swing.JPanel criteriasPanel;
    // End of variables declaration//GEN-END:variables

    
     /**
     * Obtiene todos los componentes getAllFiltros del formulario
     * @param c contenedor de los txtfield
     * @return 
     */
    private List<PanelFiltro> getAllFiltros(final Container c) {
        Component[] comps = c.getComponents();
        List<PanelFiltro> compList = new ArrayList<PanelFiltro>();
        for (Component comp : comps) {
            if (comp instanceof PanelFiltro) {
                compList.add((PanelFiltro) comp);
            }
            if (comp instanceof Container) {
                compList.addAll(getAllFiltros((Container) comp));
            }
        }
        return compList;
    }
}
